﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using EkejiWeb.Model;
using EkejiWeb.BLL;

namespace EkejiWeb.Web.Login
{
    public partial class Login : System.Web.UI.Page
    {
        AdminModel adminmodel = new AdminModel();
        UserVipModel uservipmodel = new UserVipModel();

        AdminBLL adminbll = new AdminBLL();
        UserVipBLL uservipbll = new UserVipBLL();
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!Page.IsPostBack)
            {
                if (Session["tbName"] != null && Session["tbPsw"]!=null)
                {
                    this.tbName.Text = Session["tbName"].ToString();
                    this.tbPsw.Text = Session["tbPsw"].ToString();
                }
            }
        }
        
        protected void cbPsw_CheckedChanged(object sender, EventArgs e)
        {
            if (cbPsw.Checked == true)
            {
                Session["tbName"] = this.tbName.Text;
                Session["tbPsw"] = this.tbPsw.Text;
            }
        }
        protected void btnLogin_Click(object sender, EventArgs e)
        {
            //ToUpper()是不区分大小写的方法
            string code = txtCode.Text.Trim().ToUpper();
            //从后台获取code的正确的值
            string rightCode = Session["Code"].ToString();

            if (code != rightCode)
            {
                Response.Write("<Script Language=JavaScript>alert('验证码输入错误！');</Script>");
            }
            int id=0;
            string type = "";
            //根据第一个类型，来确定进去那个表
            Boolean flag = false;
            string typeFirst = ddlUser.SelectedItem.Text.Trim();
            if (typeFirst == "管理员")
            {
                adminmodel.UserName = this.tbName.Text.Trim();
                adminmodel.PassWord = this.tbPsw.Text.Trim();
                id = adminbll.SelectIDByUserName(adminmodel.UserName);
                type = adminbll.SelectTypeByID(id);
                flag = adminbll.IsLogin(adminmodel);
            }
            else if (typeFirst == "会员")
            {
                uservipmodel.UserName = this.tbName.Text.Trim();
                uservipmodel.PassWord = this.tbPsw.Text.Trim();
                flag = uservipbll.IsLogin(uservipmodel);
                id = adminbll.SelectIDByUserName(uservipmodel.UserName);
            }
            
            if (flag)
            {
                if (typeFirst == "管理员")
                {
                    Session["AdminId"] = id;
                    Session["tbName"] = this.tbName.Text;

                    if (type == "部长级")
                    {
                        Response.Redirect("~/Admin/Admin/Index.aspx");
                    }
                    else
                    {
                        Response.Redirect("~/Admin/UserVip/Index.aspx");
                    }
                }
                else if (typeFirst == "会员")
                {
                    Session["UserId"] = id;
                    Session["tbName"] = this.tbName.Text;
                    Response.Redirect("~/UserVip/UserVip/Index.aspx");
                }
            }
            else
            {
                Response.Write("<Script Language=JavaScript>alert('用户名或密码错误！');</Script>");
                //Response.Redirect("~/Login/Login.aspx");
                return;
            }
        }
    }
}